/*
 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include "hi_wifi_api.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "ohos_init.h"
#include "cmsis_os2.h"

#include <stdio.h>
#include "lwip/sockets.h"

#define APP_INIT_VAP_NUM 2
#define APP_INIT_USR_NUM 2

static struct netif *g_lwip_netif = NULL;

/* 清除ip，网关和子网掩码 */
void hi_sta_reset_addr(struct netif *pst_lwip_netif)
{
    ip4_addr_t st_gw;
    ip4_addr_t st_ipaddr;
    ip4_addr_t st_netmask;

    if (pst_lwip_netif == NULL)
    {
        printf("hisi_reset_addr::Null param of netdev\r\n");
        return;
    }

    IP4_ADDR(&st_gw, 0, 0, 0, 0);
    IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
    IP4_ADDR(&st_netmask, 0, 0, 0, 0);

    netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
}

/* 注册回调函数*/
void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
{
    if (hisi_event == NULL)
        return;

    switch (hisi_event->event)
    {
    case HI_WIFI_EVT_SCAN_DONE: // STA扫描完成
        printf("WiFi: Scan results available\n");
        break;
    case HI_WIFI_EVT_CONNECTED: // WIFI已连接
        printf("WiFi: Connected\n");
        netifapi_dhcp_start(g_lwip_netif);
        break;
    case HI_WIFI_EVT_DISCONNECTED: // WiFi断开连接
        printf("WiFi: Disconnected\n");
        netifapi_dhcp_stop(g_lwip_netif);
        hi_sta_reset_addr(g_lwip_netif);
        break;
    case HI_WIFI_EVT_WPS_TIMEOUT: // WPS事件超时
        printf("WiFi: wps is timeout\n");
        break;
    default:
        break;
    }
}

int hi_wifi_start_connect(void)
{
    int ret;
    errno_t rc;
    hi_wifi_assoc_request assoc_req = {0};

    /* copy SSID to assoc_req */
    rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, "M20P", 4);
    if (rc != EOK)
    {
        return -1;
    }

    //开放WIFI
    // assoc_req.auth = HI_WIFI_SECURITY_OPEN;

    // WIFI加密方式
    assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

    /* WIFI密码 */
    memcpy(assoc_req.key, "12345678", 8);

    // WIFI连接
    ret = hi_wifi_sta_connect(&assoc_req);
    if (ret != HISI_OK)
    {
        return -1;
    }

    return 0;
}

static void hi_wifi_start_sta(void)
{
    int ret;
    char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
    int len = sizeof(ifname);
    const unsigned char wifi_vap_res_num = APP_INIT_VAP_NUM;
    const unsigned char wifi_user_res_num = APP_INIT_USR_NUM;
    unsigned int num = WIFI_SCAN_AP_LIMIT;

    // ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num); // WiFi初始化
    // if (ret != HISI_OK)
    // {
    //     return -1;
    // }

    ret = hi_wifi_sta_start(ifname, &len); // sta初始化
    if (ret != HISI_OK)
    {
        return;
    }

    /* 注册回调函数接口*/
    ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
    if (ret != HISI_OK)
    {
        printf("register wifi event callback failed\n");
    }

    /* 获取网络接口进行IP的操作 */
    g_lwip_netif = netifapi_netif_find(ifname);
    if (g_lwip_netif == NULL)
    {
        printf("%s: get netif failed\n", __FUNCTION__);
        return;
    }

    /* 扫描WIFI */
    ret = hi_wifi_sta_scan();
    if (ret != HISI_OK)
    {
        return;
    }

    sleep(5); /* sleep 5s, waiting for scan result. */

    //创建pst_results存放WiFi扫描结果
    hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
    if (pst_results == NULL)
    {
        return;
    }

    //获取station扫描结果
    ret = hi_wifi_sta_scan_results(pst_results, &num);
    if (ret != HISI_OK)
    {
        free(pst_results);
        return;
    }

    //打印WIFI扫描结果
    for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++)
    {
        printf("SSID: %s\n", pst_results[loop].ssid);
    }
    free(pst_results);

    /* 进行WIFI连接 */
    ret = hi_wifi_start_connect();
    if (ret != 0)
    {
        return;
    }
     //启动DHCP
    if (g_lwip_netif)
    {
        dhcp_start(g_lwip_netif);
        printf("begain to dhcp\r\n");
    }
    while (1)
    {
    }
}

void wifi_demo(void)
{
    osThreadAttr_t attr;

    attr.name = "WIFITask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4096;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)hi_wifi_start_sta, NULL, &attr) == NULL)
    {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}

APP_FEATURE_INIT(wifi_demo);
